<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音乐频谱分析器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: white;
        }

        .container {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 2rem;
            box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
            border: 1px solid rgba(255, 255, 255, 0.18);
            width: 90%;
            max-width: 800px;
        }

        h1 {
            text-align: center;
            margin-bottom: 2rem;
            font-size: 2.5rem;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

        .upload-section {
            margin-bottom: 2rem;
            text-align: center;
        }

        .file-input-wrapper {
            position: relative;
            display: inline-block;
            cursor: pointer;
            background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 10px;
            padding: 1rem 2rem;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .file-input-wrapper:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .file-input {
            position: absolute;
            opacity: 0;
            width: 100%;
            height: 100%;
            cursor: pointer;
        }

        .control-panel {
            display: flex;
            justify-content: center;
            gap: 1rem;
            margin-bottom: 2rem;
            flex-wrap: wrap;
        }

        .btn {
            background: linear-gradient(45deg, #667eea, #764ba2);
            border: none;
            color: white;
            padding: 0.8rem 1.5rem;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1rem;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .visualizer-container {
            margin-bottom: 2rem;
            border-radius: 15px;
            overflow: hidden;
            background: rgba(0, 0, 0, 0.3);
            padding: 1rem;
        }

        #canvas {
            width: 100%;
            height: 300px;
            border-radius: 10px;
            background: rgba(0, 0, 0, 0.5);
        }

        .progress-container {
            margin-top: 1rem;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            height: 8px;
            overflow: hidden;
        }

        .progress-bar {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            width: 0%;
            transition: width 0.1s linear;
        }

        .info-panel {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            padding: 1rem;
            margin-top: 1rem;
        }

        .loading {
            text-align: center;
            font-size: 1.2rem;
            color: #ffd700;
        }

        .error {
            text-align: center;
            color: #ff6b6b;
            background: rgba(255, 107, 107, 0.1);
            padding: 1rem;
            border-radius: 8px;
            margin: 1rem 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🎵 音乐频谱分析器</h1>
        
        <div class="upload-section">
            <div class="file-input-wrapper">
                <input type="file" id="audioFile" class="file-input" accept="audio/*">
                <span>📁 选择音频文件</span>
            </div>
        </div>

        <div class="control-panel">
            <button id="playBtn" class="btn" disabled>▶️ 播放</button>
            <button id="pauseBtn" class="btn" disabled>⏸️ 暂停</button>
            <button id="stopBtn" class="btn" disabled>⏹️ 停止</button>
            <button id="analyzeBtn" class="btn" disabled>🔬 分析频谱</button>
        </div>

        <div class="visualizer-container">
            <canvas id="canvas"></canvas>
            <div class="progress-container">
                <div class="progress-bar" id="progressBar"></div>
            </div>
        </div>

        <div class="info-panel">
            <div id="status">请选择一个音频文件开始...</div>
            <div id="fileInfo"></div>
        </div>
    </div>

    <!-- 可视化器系统 -->
    <script src="visualizer-interface.js"></script>
    <script src="visualizers/piano-keys.js"></script>
    <script src="visualizers/circular-fountain.js"></script>
    <script src="visualizers/bouncing-balls.js"></script>
    <script src="visualizers/keyboard-lights.js"></script>
    <script src="visualizers/wave-spectrum.js"></script>
    <script src="visualizers/3d-bars.js"></script>
    <script src="visualizers/star-field.js"></script>
    <script src="visualizers/circular-pulse.js"></script>
    <script src="visualizer-selector.js"></script>
    
    <!-- 主应用脚本 -->
    <script src="script.js"></script>
    
    <!-- 初始化可视化器系统 -->
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            console.log('DOM loaded, initializing visualizer system...');
            
            // 检查可视化器注册情况
            console.log('Visualizer registrations:', window.visualizerRegistrations?.length || 0);
            
            // 等待主应用初始化完成后再设置可视化器
            let retryCount = 0;
            const maxRetries = 10;
            
            const initVisualizers = () => {
                if (window.audioAnalyzer && window.audioAnalyzer.visualizerManager) {
                    console.log('Audio analyzer found, setting up selector...');
                    
                    try {
                        const selector = new VisualizerSelector();
                        selector.setManager(window.audioAnalyzer.visualizerManager);
                        
                        // 默认选择第一个可视化器
                        const visualizers = window.audioAnalyzer.visualizerManager.getAvailableVisualizers();
                        console.log('Available visualizers:', visualizers.map(v => v.id));
                        
                        if (visualizers.length > 0) {
                            selector.selectVisualizer(visualizers[0].id);
                            console.log('Selected default visualizer:', visualizers[0].id);
                        } else {
                            console.warn('No visualizers available');
                        }
                    } catch (error) {
                        console.error('Error setting up visualizer selector:', error);
                    }
                } else {
                    retryCount++;
                    if (retryCount < maxRetries) {
                        console.log(`Retrying initialization... (${retryCount}/${maxRetries})`);
                        setTimeout(initVisualizers, 200);
                    } else {
                        console.error('Failed to initialize visualizer system after', maxRetries, 'attempts');
                    }
                }
            };
            
            // 延迟启动以确保所有脚本都已加载
            setTimeout(initVisualizers, 300);
        });
    </script>
</body>
</html>
